/*
 * Copyright (C) 2011 by ayoung <ayoung.p@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.peaceb.ruletherain;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import android.os.Build;

public class HttpHelper {
    static {
        disableConnectionReuseIfNecessary();
    }
    
    public static int HttpGet(WeatherInfo city) {
        int ret = C.ERROR;
        
        try {
            URL url = new URL(C.QUERY_URL + URLEncoder.encode(city.cityName));
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            
            if (conn != null) {
                StringBuilder sb = new StringBuilder();
                
                conn.setConnectTimeout(C.CONNECT_TIMEOUT);
                conn.setUseCaches(false);
                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(conn.getInputStream()));
                    while (true) {
                        String line = br.readLine();
                        if (line == null) {
                            break;
                        }
                        sb.append(line);
                    }
                    br.close();
                    city.rawResult = sb.toString();
                    if (city.rawResult.length() > 0) {
                        ret = C.SUCCESS;
                    }
                    else {
                        ret = C.ERROR;
                    }
                }
                conn.disconnect();
            }
        } 
        catch (MalformedURLException e) {
            Trace.e(e.toString());
            return C.ERROR;
        } 
        catch (IOException e) {
            Trace.e(e.toString());
            return C.ERROR;
        }
        
        return ret;
    }
    
    private static void disableConnectionReuseIfNecessary() {
        /*
         * http://android-developers.blogspot.com/2011/09/androids-http-clients.html
         * HTTP connection reuse which was buggy pre-froyo
         */
        if (Integer.parseInt(Build.VERSION.SDK) < 8 /* Build.VERSION_CODES.FROYO */) {
            System.setProperty("http.keepAlive", "false");
        }
    }
}
